﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DAO
{
    public class ChiTietToaThuocDAO
    {
        public static DataTable LayDanhSachChiTietToaThuoc()
        {
            string sql;
            sql = "SELECT * FROM [ChiTietToaThuoc]";
            return DataAccess.ExecQuery(sql);
        }

        public static void ThemChiTietToaThuoc(ChiTietToaThuocDTO obj)
        {
            string sql = "";
            sql = string.Format(@"INSERT INTO [ChiTietToaThuoc](MaThuoc, MaTT, SoLuong, CongDung, CachDung) VALUES(N'{0}', N'{1}', N'{2}', N'{3}', N'{4}')",
                                                obj.MaThuoc, obj.MaTT, obj.SoLuong, obj.CongDung, obj.CachDung);
            DataAccess.ExecNonQuery(sql);
        }

        public static void SuaChiTietToaThuoc(ChiTietToaThuocDTO obj)
        {
            string sql = "";
            sql = string.Format(@"UPDATE [ChiTietToaThuoc]
                                SET SoLuong = N'{0}', CongDung = N'{1}', CachDung = N'{2}'
                                WHERE MaThuoc = {3} and MaTT = {4}", obj.SoLuong, obj.CongDung, obj.CachDung, obj.MaThuoc, obj.MaTT);
            DataAccess.ExecNonQuery(sql);
        }

        public static void XoaChiTietToaThuoc(int mathuoc, int matt)
        {
            string sql;
            sql = string.Format("DELETE [ChiTietToaThuoc] WHERE MaThuoc = {0} and MaTT = {1}",  mathuoc, matt);
            DataAccess.ExecNonQuery(sql);
        }

        public static ChiTietToaThuocDTO LayThongTinChiTietToaThuocTheoMaChiTietToaThuoc(int maChiTietToaThuoc)
        {
            ChiTietToaThuocDTO kq = new ChiTietToaThuocDTO();
            string sql;
            sql = @"select * FROM [ChiTietToaThuoc]
                    where MaChiTietToaThuoc = " + maChiTietToaThuoc;
            DataTable dt = DataAccess.ExecQuery(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                kq.MaThuoc = Convert.ToInt32(dt.Rows[0]["MaThuoc"]);
                kq.MaTT = Convert.ToInt32(dt.Rows[0]["MaTT"]);
                kq.SoLuong = Convert.ToInt32(dt.Rows[0]["SoLuong"]);
                kq.CongDung = Convert.ToString(dt.Rows[0]["CongDung"]);
                kq.CachDung = Convert.ToString(dt.Rows[0]["CachDung"]);                
            }
            return kq;
        }

        public static DataTable LayDanhSachChiTietToaThuocTheoMaPKB(string sql)
        {
            return DataAccess.ExecQuery(sql);
        }
    }
}
